﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto.WCSTaskDto;
using Zocono.WCS.Infrastructure.Device;

namespace Zocono.WCS.Domain.SocketServiceDomain
{
    public class SocketClientInfoService : ISocketClientInfoService
    {
        private readonly ISocketClientInfoRepository _ISocketClientInfoRepository;
        private readonly IMapper _IMapper;

        private DeviceSocketClient deviceSocketClient;

        public SocketClientInfoService(ISocketClientInfoRepository socketClientInfoRepository, IMapper iMapper)
        {
            _ISocketClientInfoRepository = socketClientInfoRepository;
            _IMapper = iMapper;
        }

        public Task<bool> DeleteSocketClientInfosAsync(long socketId)
        {
            throw new NotImplementedException();
        }

        public Task<bool> DeleteSocketClientInfosAsync(SocketClientInfo socketInfo)
        {
            throw new NotImplementedException();
        }

        Device device;
        public async Task<List<SocketClientInfo>> GetSocketClientInfosAsync()
        {
            try
            {
                List<SocketClientInfo> res = await _ISocketClientInfoRepository.GetListAsync();
                List<SocketClientInfoDto> socketClientInfoDtos = new List<SocketClientInfoDto>();

                //SocketClientInfo res1 = await _ISocketClientInfoRepository.AsQueryable().FirstAsync();
                SocketClientInfoDto socketClientInfoDtos1 = new SocketClientInfoDto();

                foreach (var item in res)
                {
                    _IMapper.Map(item, socketClientInfoDtos1);
                    socketClientInfoDtos.Add(socketClientInfoDtos1);
                }     
                _IMapper.Map(res, socketClientInfoDtos);
                if (deviceSocketClient == null)
                {
                    deviceSocketClient = new DeviceSocketClient(socketClientInfoDtos);
                    device = deviceSocketClient;
                    Init();
                }
                return res;

            }
            catch (Exception ex)
            {

                return null;
            }
        }

        public Task<bool> UpdateSocketClientInfoAsync(SocketClientInfo socketInfo)
        {
            throw new NotImplementedException();
        }

        private void Init()
        {
            device.Init();
            device.Start();
        }

    }
}
